package com.rds.workflow.service.impl;

import com.rds.common.core.redis.RedisCache;
import com.rds.system.domain.RdaPolicy;
import com.rds.system.domain.RdaState;
import com.rds.system.domain.RdaType;
import com.rds.system.mapper.RdaPolicyMapper;
import com.rds.system.mapper.RdaStateMapper;
import com.rds.system.mapper.RdaTypeMapper;
import com.rds.workflow.domain.RdaWorkflowTemplete;
import com.rds.workflow.mapper.RdaNodeTempleteMapper;
import com.rds.workflow.mapper.RdaWorkflow2NodeTempleteRelMapper;
import com.rds.workflow.mapper.RdaWorkflowTempleteMapper;
import com.rds.workflow.service.IRdaWorkflowTempleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RdaWorkflowTempleteServiceImpl implements IRdaWorkflowTempleteService {
    @Autowired
    private RdaWorkflowTempleteMapper rdaWorkflowTempleteMapper;

    @Autowired
    private RdaNodeTempleteMapper rdaNodeTempleteMapper;

    @Autowired
    private RdaTypeMapper rdaTypeMapper;

    @Autowired
    private RdaPolicyMapper rdaPolicyMapper;

    @Autowired
    private RdaStateMapper rdaStateMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RdaWorkflow2NodeTempleteRelMapper rdaWorkflow2NodeTempleteRel;
    /**
     * 项目启动时，初始化类型、策略、状态到缓存
     */
    @PostConstruct
    public void init(){
        List<RdaType> typeList =rdaTypeMapper.selectRdaTypeList(new RdaType());//类型
        List<RdaPolicy> policyList =rdaPolicyMapper.selectRdaPolicyList(new RdaPolicy());//策略
        List<RdaState>   stateList=rdaStateMapper.selectRdaStateList(new RdaState());//状态
        Map<Long,String> typeMap=new HashMap<Long,String>();
        Map<Long,String> policyMap=new HashMap<Long,String>();
        Map<Long,String> stateMap=new HashMap<Long,String>();
        for (RdaType rdaType:typeList) {
            typeMap.put(rdaType.getId(),rdaType.getDescription());
        }
        for (RdaPolicy rdaPolicy:policyList) {
            policyMap.put(rdaPolicy.getId(),rdaPolicy.getDescription());
        }
        for (RdaState rdaState:stateList) {
            stateMap.put(rdaState.getId(),rdaState.getDescription());
        }
        redisCache.setCacheObject("typeList",typeMap);
        redisCache.setCacheObject("policyList",policyMap);
        redisCache.setCacheObject("stateList",stateMap);
    }
    /**
     * 查询流程模板
     *
     * @param id 项目模板ID
     * @return 项目模板
     */
    @Override
    public RdaWorkflowTemplete selectRdaWorkflowTempleteById(Long id) {
        return rdaWorkflowTempleteMapper.selectRdaWorkflowTempleteById(id);
    }

    /**
     * 查询流程模板列表
     *
     * @param rdaWorkflowTemplete 项目流程
     * @return 项目模板集合
     */
    @Override
    public List<RdaWorkflowTemplete> selectRdaWorkflowTempleteList(RdaWorkflowTemplete rdaWorkflowTemplete) {
        return rdaWorkflowTempleteMapper.selectRdaWorkflowTempleteList(rdaWorkflowTemplete);
    }

    /**
     * 新增流程模板
     *
     * @param rdaWorkflowTemplete 流程模板
     * @return 结果
     */
    @Override
    public int insertRdaWorkflowTemplete(RdaWorkflowTemplete rdaWorkflowTemplete) {
        return rdaWorkflowTempleteMapper.insertRdaWorkflowTemplete(rdaWorkflowTemplete);
    }

    /**
     * 修改流程模板
     *
     * @param rdaWorkflowTemplete 流程模板
     * @return 结果
     */
    @Override
    public int updateRdaWorkflowTemplete(RdaWorkflowTemplete rdaWorkflowTemplete) {
        return rdaWorkflowTempleteMapper.updateRdaWorkflowTemplete(rdaWorkflowTemplete);
    }

    /**
     * 批量删除流程模板
     *
     * @param ids 需要删除的流程模板ID
     * @return 结果
     */
    @Override
    public int deleteRdaWorkflowTempleteByIds(Long[] ids) {
        rdaNodeTempleteMapper.deleteRdaNodeTempleteByWorkflowIds(ids);
        rdaWorkflow2NodeTempleteRel.deleteRdaWorkflow2NodeTempleteRelByWorkflowIds(ids);
        return rdaWorkflowTempleteMapper.deleteRdaWorkflowTempleteByIds(ids);
    }
}
